package com.block.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.model.vo.AddressInfoVO;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AddressInfo;
import com.block.db.mapper.AddressInfoMapper;
import com.block.common.service.IAddressInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.db.util.AddressTypeEnum;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-24
 */
@Service
public class AddressInfoServiceImpl extends ServiceImpl<AddressInfoMapper, AddressInfo> implements IAddressInfoService {

    @Resource
    private RedisTemplate<String, AddressInfo> redisAddressInfoTemplate;

    private  static  final  String ADDRESS_INFO_KEY_PREFIX="ADDRESSINFO:CITYCODE:";
    private  static  final  Long ADDRESS_INFO_KEY_TIME_OUT=24L;

    @Override
    public AddressInfo getAddressInfoByCityCode(String cityCode){
        String redisKey = ADDRESS_INFO_KEY_PREFIX+cityCode;
        AddressInfo addressInfo= redisAddressInfoTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(addressInfo)){
            addressInfo = this.baseMapper.selectById(cityCode);
            if(ObjectUtil.isNotEmpty(addressInfo) && ObjectUtil.equals(AddressTypeEnum.CITY.getValue(), addressInfo.getType())) {
                redisAddressInfoTemplate.opsForValue().set(redisKey, addressInfo, ADDRESS_INFO_KEY_TIME_OUT, TimeUnit.HOURS);
            }else {
                return null;
            }
        }
        return addressInfo;
    }

    /**
     * 城市名称的缓存
     * @param city
     * @return
     */
    @Override
    public AddressInfo getAddresInfoByCity(String city) {
        // TODO: 2024/1/9 加redis 缓存
        return this.baseMapper.selectEntityByCityName(city);
    }

    @Override
    public ResponseVo getAllAddressInfo() {
        List<AddressInfoVO> resultList = new ArrayList<>();
        // TODO: 2024/4/15 地址信息
        List<AddressInfo> addressInfoList = this.baseMapper.selectListByProvince();
        for (AddressInfo addressInfo : addressInfoList) {
            AddressInfoVO addressInfoVO = new AddressInfoVO();
            addressInfoVO.setCodeInfo(addressInfo.getCityCode());
            addressInfoVO.setNameInfo(addressInfo.getCity());

            List<AddressInfoVO> itemList = new ArrayList<>();
            List<AddressInfo> cityList = this.baseMapper.selectCityListByProvinceCode(addressInfo.getCityCode());

            for (AddressInfo info : cityList) {
                AddressInfoVO cityVo = new AddressInfoVO();
                cityVo.setCodeInfo(info.getCityCode());
                cityVo.setNameInfo(info.getCity());
                itemList.add(cityVo);
            }
            addressInfoVO.setItemInfoList(itemList);
            resultList.add(addressInfoVO);
        }

        JSONObject returnObject = new JSONObject();
        returnObject.put("dataList", resultList);

        return new ResponseVo(AppRspCodeConstant.SUCCESS,returnObject );
    }

}
